package okl.tdf.components
{
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.events.FlexEvent;
	import mx.events.StateChangeEvent;
	
	import okl.tdf.components.interfaces.IInteractionDisplay;
	import okl.tdf.model.InteractionsPerCountry;
	
	import org.flexunit.asserts.assertEquals;
	import org.flexunit.asserts.assertNotNull;
	import org.flexunit.asserts.assertNull;
	import org.flexunit.asserts.fail;
	import org.flexunit.async.Async;
	import org.fluint.uiImpersonation.UIImpersonator;

	/**
	 * Test for InteractionDisplay 
	 */
	[Before(async, ui)]
	public class InteractionDisplayTest
	{		

		private var ui:InteractionDisplay;
		private var testDataSet:ArrayCollection;
		
		[Before(async,ui)]
		public function setUp():void
		{
			ui = new InteractionDisplay();
			
			testDataSet = new ArrayCollection([
				createVO("UK"),
				createVO("UA"),
				createVO("USA"),
				createVO("UA")
			]);
		
			Async.proceedOnEvent(this, ui, FlexEvent.CREATION_COMPLETE);
			UIImpersonator.addChild(ui as InteractionDisplay);
		}
		
		
		[Test]
		public function testInitialState():void
		{
			assertNull(ui.dataProvider);			
			assertEquals("normal", ui.currentState);
		}
		
		[Test(async,ui)]
		public function testDataSetState():void
		{
			Async.handleEvent(this, ui, StateChangeEvent.CURRENT_STATE_CHANGE, testDataSetState_Callback, 500, "dataFeed", handleTimeout);
			
			ui.dataProvider = testDataSet;
			assertNotNull(ui.dataProvider);		
			assertEquals(testDataSet.length, ui.dataProvider.length);	

		}
		
		[Test(async,ui)]
		public function testDataProvider():void
		{
			Async.handleEvent(this, ui, "dataChange", testDataProvider_Callback, 500, testDataSet, handleTimeout);
			ui.dataProvider = 	testDataSet;	
		}
		
		//{region UTILS
		protected function handleTimeout( event:Object ):void
		{
			fail("Event Timeout");
		}
		
		private function testDataProvider_Callback(event:Event, expected:Object):void{
			assertEquals(expected, ui.dataProvider);
		}
		
		private function 	testDataSetState_Callback(event:Event, expected:Object):void{
			assertEquals(expected, ui.currentState);
		}
		
	
		
		
		private function createVO(code:String="UK", time1:Date=null, time2:Date=null, count:Number=1):InteractionsPerCountry{
			var result:InteractionsPerCountry=new InteractionsPerCountry(code);
			result.firstResultTime=time1 || new Date();
			result.lastResultTime=time2 || new Date(result.firstResultTime.time+5000);
			result.totalCount=count;
			return result;
		}
		//}endregion

		
		[After(async,ui)]
		public function tearDown():void
		{
			ui=null;
			testDataSet=null;
		}

		
		
	}
}